﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using DAL.DBManagers;
using DAL.Models;
using DAL.Models.Enums;

public partial class SaleManager : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        lblAdditionalParamForSaleError.Visible = lblAddNavigationSaleError.Visible = lblVesselCodeAddError.Visible = false;

        var user = Session["User"] as UserHelper;
        VesselCodeSaleDS.UpdateParameters["UserID"].DefaultValue =
        NavigationModeSaleDS.UpdateParameters["UserID"].DefaultValue =
        AdditionalParamForSaleSaleDS.UpdateParameters["UserID"].DefaultValue =
        NavigationModeSaleDS.DeleteParameters["UserID"].DefaultValue =
        VesselCodeSaleDS.DeleteParameters["UserID"].DefaultValue =
        AdditionalParamForSaleSaleDS.DeleteParameters["UserID"].DefaultValue = user.UserID.ToString();
    }

    protected void Page_PreRender(object sender, EventArgs e)
    {
        txtVesselCode.Text = txtNavigationModeDiscount.Text = txtAdditionalParamForSale.Text = String.Empty;
    }

    protected void btnAddNavigationModeSale_Click(object sender, EventArgs e)
    {
        using (SalesManager manager = new SalesManager())
        {
            Page.Validate("NavigationValidation");
            if (!Page.IsValid)
                return;

            var thingId = Guid.Parse(ddlNavigationMode.SelectedValue);
            var serviceid = Guid.Parse(Request.QueryString["id"]);

            if (manager.Find(f => f.IsActive && f.ThingID == thingId && f.ServiceID == serviceid).Any())
            {
                lblAddNavigationSaleError.Visible = true;
                return;
            }
            manager.Add(new Sale
            {
                SaleID = Guid.NewGuid(),
                SaleTypeID = (int)SaleTypes.NAVIGATION_MODE,
                ServiceID = serviceid,
                ThingID = thingId,
                Discount = Double.Parse(txtNavigationModeDiscount.Text.Replace(",", ".")),
                IsActive = true,
                CreateDate = DateTime.Now,
                CreatedBy = (Session["User"] as UserHelper).UserID
            });

            manager.SaveChanges();

            NavigationSaleGirdView.DataBind();
        }
    }

    protected void btnVesselCodeSale_Click(object sender, EventArgs e)
    {
        Page.Validate("VesselCodeValidation");
        if (!Page.IsValid)
            return;

        using (SalesManager manager = new SalesManager())
        {
            var thingId = Guid.Parse(ddlVesselCode.SelectedValue);
            var serviceid = Guid.Parse(Request.QueryString["id"]);

            if (manager.Find(f => f.IsActive && f.ThingID == thingId && f.ServiceID == serviceid).Any())
            {
                lblVesselCodeAddError.Visible = true;
                return;
            }
            manager.Add(new Sale
            {
                SaleID = Guid.NewGuid(),
                SaleTypeID = (int)SaleTypes.VESSELCODE,
                ServiceID = serviceid,
                ThingID = thingId,
                Discount = Double.Parse(txtVesselCode.Text.Replace(",", ".")),
                IsActive = true,
                CreateDate = DateTime.Now,
                CreatedBy = (Session["User"] as UserHelper).UserID
            });

            manager.SaveChanges();

            VesselCodeSaleGridView.DataBind();
        }
    }

    protected void btnAdditionalParamForSaleSale_Click(object sender, EventArgs e)
    {
        Page.Validate("AdditionalParamForSale");
        if (!Page.IsValid)
            return;

        using (SalesManager manager = new SalesManager())
        {
            var thingId = Guid.Parse(ddlAdditionalParamForSale.SelectedValue);
            var serviceid = Guid.Parse(Request.QueryString["id"]);

            if (manager.Find(f => f.IsActive && f.ThingID == thingId && f.ServiceID == serviceid).Any())
            {
                lblAdditionalParamForSaleError.Visible = true;
                return;
            }
            manager.Add(new Sale
            {
                SaleID = Guid.NewGuid(),
                SaleTypeID = (int)SaleTypes.ADDITIONALPARAMFORSALE,
                ServiceID = serviceid,
                ThingID = thingId,
                Discount = Double.Parse(txtAdditionalParamForSale.Text),
                IsActive = true,
                CreateDate = DateTime.Now,
                CreatedBy = (Session["User"] as UserHelper).UserID
            });

            manager.SaveChanges();

            AdditionalParamForSaleGridView.DataBind();
        }
    }
}